<?php

class Admin_Model_Admin extends Zend_Db_Table_Abstract
{
    protected $_name = 'admin';
    public function getDataByAdminId($adminId){
    	$query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('a'=>$this->_name),array('a.adminId','a.username','a.password','a.status','a.email','a.fullName','a.sessionId'))
                        ->where('adminId = ?',(int)$adminId);                                
        return $this->fetchRow($query);        
    }    
    public function getDataByUsername($username){        
    	$query = $this->select()
                        ->from(array('a'=>$this->_name),array('a.adminId','a.username','a.password','a.status','a.email','a.fullName','a.sessionId'))
                        ->where("username = '".$username."'");                                
        return $this->fetchRow($query);
    }
    public function updateStatus($status, $userId){
        $data['status']=$status;
        return $this->update($data, 'userId=' . (int)$userId );
    }
    public function updateSession($sessionId, $adminId){
        $data['sessionId']=$sessionId;
        return $this->update($data,"adminId=".(int)$adminId);
    }
    public function updatePass($userId, $password){
        $data['password'] = md5($password);
        return $this->update($data, 'userId=' . (int)$userId );
    }
    public function getAllData(){
    	$query = $this->select()
                        ->from($this->_name,array('userId', 'username', 'status'))
                        ->where("userId > 1");
    	return $this->fetchAll($query);
    }
    public function addData($username, $password, $status){
    	$data = array('username' => $username, 'password' => md5($password), 'status' => $status);
        $idAdd = $this->insert($data);
        return $idAdd;
    }
    public function deleteData($userId){
        $this->delete('userId =' . (int)$userId);  
    }
    public function updateData($userId, $username, $password, $status){
    	if($password && strlen($password) >= 6){
    		 $data = array('username' => $username ,'password' => md5($password), 'status' => $status);
    	}else{
    		 $data = array('username' => $username, 'status' => $status);
    	}
        return $this->update($data, 'userId = ' . (int)$userId);
    }
    
  
}

